ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/item.C
Revision: 1.81
Committed: Tue Oct 20 07:18:11 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_82
Changes since 1.80: +16 -19 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * the terms of the Affero GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the Affero GNU General Public License
19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */
24
25 /**
26 * \file
27 * Client/server logic.
28 *
29 * \date 2003-12-02
30 *
31 * This contains item logic for client/server. It doesn't contain
32 * the actual commands that send the data, but does contain
33 * the logic for what items should be sent.
34 */
35
36 #include <global.h>
37 #include <object.h>
38 #include <sproto.h>
39
40 /** This is the maximum number of bytes we expect any one item to take up */
41 #define MAXITEMLEN 300
42
43 /*******************************************************************************
44 *
45 * Functions related to sending object data to the client.
46 *
47 ******************************************************************************/
48
49 /**
50 * This is a similar to query_name, but returns flags
51 * to be sent to client.
52 */
53 unsigned int
54 query_flags (object *op)
55 {
56 unsigned int flags = 0;
57
58 if (QUERY_FLAG (op, FLAG_APPLIED))
59 switch (op->type)
60 {
61 case BOW:
62 case WAND:
63 case ROD:
64 case HORN:
65 flags = a_readied;
66 break;
67 case WEAPON:
68 flags = a_wielded;
69 break;
70 case SKILL:
71 case ARMOUR:
72 case HELMET:
73 case SHIELD:
74 case RING:
75 case BOOTS:
76 case GLOVES:
77 case AMULET:
78 case GIRDLE:
79 case BRACERS:
80 case CLOAK:
81 flags = a_worn;
82 break;
83 case CONTAINER:
84 flags = a_active;
85 break;
86 default:
87 flags = a_applied;
88 break;
89 }
90
91 if (op->is_open_container ())
92 flags |= F_OPEN;
93
94 if (QUERY_FLAG (op, FLAG_KNOWN_CURSED))
95 {
96 if (QUERY_FLAG (op, FLAG_DAMNED))
97 flags |= F_DAMNED;
98 else if (QUERY_FLAG (op, FLAG_CURSED))
99 flags |= F_CURSED;
100 }
101
102 if (QUERY_FLAG (op, FLAG_KNOWN_MAGICAL) && !QUERY_FLAG (op, FLAG_IDENTIFIED))
103 flags |= F_MAGIC;
104 if (QUERY_FLAG (op, FLAG_UNPAID))
105 flags |= F_UNPAID;
106 if (QUERY_FLAG (op, FLAG_INV_LOCKED))
107 flags |= F_LOCKED;
108
109 return flags;
110 }
111
112 /* Used in the send_look to put object head into packet
113 * sl for socket ns. Need socket to know if we need to send
114 * animation of face to the client.
115 */
116 static void
117 add_object_to_socklist (client &ns, packet &sl, object *head)
118 {
119 int flags = query_flags (head);
120
121 if (QUERY_FLAG (head, FLAG_NO_PICK))
122 flags |= F_NOPICK;
123
124 ns.send_face (head->face, -50);
125 ns.flush_fx ();
126
127 if (QUERY_FLAG (head, FLAG_ANIMATE) && !ns.anims_sent[head->animation_id])
128 ns.send_animation (head->animation_id);
129
130 sl << uint32 (head->count)
131 << uint32 (flags)
132 << uint32 (QUERY_FLAG (head, FLAG_NO_PICK) ? -1 : head->client_weight ())
133 << uint32 (head->face);
134
135 if (!head->custom_name)
136 {
137 const char *name;
138 uint8 *len = sl.cur++; // patch up length later
139
140 name = query_base_name (head, 0); sl << data (name, min (127, strlen (name)));
141 sl << uint8 (0);
142 name = query_base_name (head, 1); sl << data (name, min (127, strlen (name)));
143
144 *len = sl.cur - len - 1;
145 }
146 else
147 {
148 int len = min (head->custom_name.length (), 127);
149
150 sl << uint8 (len * 2 + 1)
151 << data (head->custom_name, len)
152 << uint8 (0)
153 << data (head->custom_name, len);
154 }
155
156 sl << uint16 (head->animation_id);
157
158 int anim_speed = !head->flag [FLAG_ANIMATE] ? 0
159 : head->anim_speed ? clamp (head->anim_speed, 1, 255)
160 : 1. / clamp (fabs (head->speed), 1./255., 1./1.);
161
162 sl << uint8 (anim_speed)
163 << uint32 (head->nrof);
164
165 if (ns.itemcmd == 2)
166 sl << uint16 (head->client_type);
167 }
168
169 static faceidx
170 need_face_now (player *pl, const char *name)
171 {
172 faceidx face = face_find (name, empty_face);
173
174 pl->ns->send_face (face, -50);
175 pl->ns->flush_fx ();
176
177 return face;
178 }
179
180 #define FINGER_UP "finger_up.x11"
181 #define FINGER_DOWN "finger_down.x11"
182
183 /**
184 * Send the look window. Don't need to do animations here
185 * This sends all the faces to the client, not just updates. This is
186 * because object ordering would otherwise be inconsistent.
187 */
188 void
189 esrv_draw_look (player *pl)
190 {
191 object *ob = pl->ob;
192
193 if (!pl->ns->update_look)
194 {
195 LOG (llevDebug, "esrv_draw_look called when update_look was not set (player %s)\n", &ob->name);
196 return;
197 }
198
199 pl->ns->update_look = 0;
200
201 if (QUERY_FLAG (ob, FLAG_REMOVED)
202 || !ob->map
203 || ob->map->in_memory != MAP_ACTIVE
204 || out_of_map (ob->map, ob->x, ob->y))
205 return;
206
207 pl->ns->send_packet ("delinv 0");
208
209 packet sl;
210 sl.printf ("item%d ", pl->ns->itemcmd);
211
212 sl << uint32 (0);
213
214 int start_pos = pl->ns->look_position;
215 bool dirty = false;
216
217 mapspace &ms = ob->ms ();
218
219 // manage a ring buffer of the "last FLOORBOX_PAGESIZE" items and
220 // start from the top
221 object *items [FLOORBOX_PAGESIZE];
222 int item_idx = 0, item_cnt = 0;
223 int pos = 0;
224
225 // find our items by walking down
226 object *item = ms.top;
227
228 for (; item && item_cnt < FLOORBOX_PAGESIZE; item = item->below)
229 {
230 if (!item->client_visible ())
231 continue;
232
233 if (++pos < start_pos)
234 continue;
235
236 // record item
237 items [item_idx] = item; item_idx = item_idx < FLOORBOX_PAGESIZE ? item_idx + 1 : 0;
238 item_cnt++;
239
240 // stop at first floor
241 if (item->flag [FLAG_IS_FLOOR])
242 {
243 // we are finished, don't append "next group of items"
244 // by setting item to ms.bot it becomes zero which is checked after the while loop
245 item = ms.bot;
246 }
247 }
248
249 // see if there are more if we cared - we ignore invisible objects
250 if (item)
251 {
252 /* What we basically do is make a 'fake' object - when the user applies it,
253 * we notice the special tag the object has, and act accordingly.
254 */
255 sl << uint32 (0x80000000 | (start_pos + FLOORBOX_PAGESIZE))
256 << uint32 (0)
257 << uint32 ((uint32) - 1)
258 << uint32 (need_face_now (pl, FINGER_DOWN))
259 << data8 ("Apply this to see the items below")
260 << uint16 (0)
261 << uint8 (0)
262 << uint32 (0);
263
264 if (pl->ns->itemcmd == 2)
265 sl << uint16 (0);
266
267 dirty = true;
268 }
269
270 // now send out all items in the ring buffer in reverse order
271 while (item_cnt)
272 {
273 --item_cnt;
274 item_idx = (item_idx ? item_idx : FLOORBOX_PAGESIZE) - 1;
275 object *item = items [item_idx];
276
277 add_object_to_socklist (*pl->ns, sl, item->head_ ());
278
279 dirty = true;
280
281 // if packet got too large, send it and begin a new one
282 if (sl.length () > MAXSOCKBUF - MAXITEMLEN)
283 {
284 pl->ns->send_packet (sl);
285
286 sl.reset ();
287 sl.printf ("item%d ", pl->ns->itemcmd);
288 sl << uint32 (0);
289
290 dirty = false;
291 }
292 }
293
294 if (start_pos)
295 {
296 sl << uint32 (0x80000000 | (start_pos - FLOORBOX_PAGESIZE))
297 << uint32 (0)
298 << sint32 (-1)
299 << uint32 (need_face_now (pl, FINGER_UP))
300 << data8 ("Apply this to see the items higher up")
301 << uint16 (0)
302 << uint8 (0)
303 << uint32 (0);
304
305 if (pl->ns->itemcmd == 2)
306 sl << uint16 (0);
307
308 dirty = true;
309 }
310
311 if (dirty)
312 pl->ns->send_packet (sl);
313 }
314
315 /**
316 * Sends whole inventory.
317 */
318 void
319 esrv_send_inventory (object *pl, object *op)
320 {
321 if (!pl->contr->ns)//D
322 return;
323
324 int got_one = 0;
325
326 pl->contr->ns->send_packet_printf ("delinv %d", op->count);
327
328 packet sl;
329 sl.printf ("item%d ", pl->contr->ns->itemcmd);
330
331 sl << uint32 (op->count);
332
333 for (object *tmp = op->inv; tmp; tmp = tmp->below)
334 {
335 object *head;
336
337 if (tmp->head)
338 head = tmp->head;
339 else
340 head = tmp;
341
342 if (head->client_visible ())
343 {
344 add_object_to_socklist (*pl->contr->ns, sl, head);
345
346 got_one++;
347
348 /* IT is possible for players to accumulate a huge amount of
349 * items (especially with some of the bags out there) to
350 * overflow the buffer. IF so, send multiple item commands.
351 */
352 if (sl.length () > MAXSOCKBUF - MAXITEMLEN)
353 {
354 pl->contr->ns->send_packet (sl);
355
356 sl.reset ();
357 sl.printf ("item%d ", pl->contr->ns->itemcmd);
358 sl << uint32 (op->count);
359 got_one = 0;
360 }
361 }
362 }
363
364 if (got_one)
365 pl->contr->ns->send_packet (sl);
366 }
367
368 /**
369 * Updates object *op for player *pl.
370 *
371 * flags is a list of values to update
372 * to the client (as defined in newclient.h - might as well use the
373 * same value both places.
374 */
375 void
376 esrv_update_item (int flags, object *pl, object *op)
377 {
378 /* If we have a request to send the player item, skip a few checks. */
379 if (op != pl && !op->client_visible ())
380 return;
381
382 client *ns = pl->contr->ns;
383 if (!ns)
384 return;
385
386 packet sl ("upditem");
387
388 sl << uint8 (flags);
389
390 op = op->head_ ();
391
392 sl << uint32 (op->count);
393
394 if (flags & UPD_LOCATION)
395 sl << uint32 (op->env ? op->env->count : 0);
396
397 if (flags & UPD_FLAGS)
398 sl << uint32 (query_flags (op));
399
400 if (flags & UPD_WEIGHT)
401 {
402 sint32 weight = op->client_weight ();
403
404 if (op == pl)
405 ns->last_weight = weight;
406
407 sl << uint32 (weight);
408 }
409
410 if (flags & UPD_FACE)
411 {
412 ns->send_face (op->face, -50);
413 ns->flush_fx ();
414 sl << uint32 (op->face);
415 }
416
417 if (flags & UPD_NAME)
418 {
419 int len;
420 const char *item_p;
421 char item_n[127 * 2];
422
423 if (!op->custom_name)
424 {
425 len = assign (item_n, query_base_name (op, 0), 127);
426 item_p = query_base_name (op, 1);
427 }
428 else
429 {
430 len = assign (item_n, &op->custom_name, 127);
431 item_p = &op->custom_name;
432 }
433
434 len += assign (item_n + len, item_p, 127);
435
436 sl << data8 (item_n, len);
437 }
438
439 if (flags & UPD_ANIM)
440 sl << uint16 (op->animation_id);
441
442 if (flags & UPD_ANIMSPEED)
443 {
444 int anim_speed = 0;
445
446 if (QUERY_FLAG (op, FLAG_ANIMATE))
447 {
448 if (op->anim_speed)
449 anim_speed = op->anim_speed;
450 else
451 {
452 if (fabs (op->speed) < 0.001)
453 anim_speed = 255;
454 else if (fabs (op->speed) >= 1.0)
455 anim_speed = 1;
456 else
457 anim_speed = (int) (1.0 / fabs (op->speed));
458 }
459
460 if (anim_speed > 255)
461 anim_speed = 255;
462 }
463
464 sl << uint8 (anim_speed);
465 }
466
467 if (flags & UPD_NROF)
468 sl << uint32 (op->nrof);
469
470 pl->contr->ns->send_packet (sl);
471 }
472
473 /**
474 * Sends item's info to player.
475 */
476 void
477 esrv_send_item (object *pl, object *op)
478 {
479 if (!pl->contr->ns)
480 return;
481
482 /* We only send 'visible' objects to the client, and sometimes the player */
483 if (!op->client_visible () && op->type != PLAYER)
484 return;
485
486 packet sl;
487
488 sl.printf ("item%d ", pl->contr->ns->itemcmd);
489
490 op = op->head_ ();
491
492 sl << uint32 (op->env ? op->env->count : 0);
493
494 add_object_to_socklist (*pl->contr->ns, sl, op);
495
496 pl->contr->ns->send_packet (sl);
497 }
498
499 /**
500 * Tells the client to delete an item.
501 */
502 void
503 esrv_del_item (player *pl, int tag)
504 {
505 if (!pl->ns)
506 return;
507
508 packet sl ("delitem");
509
510 sl << uint32 (tag);
511
512 pl->ns->send_packet (sl);
513 }
514
515
516 /*******************************************************************************
517 *
518 * Client has requested us to do something with an object.
519 *
520 ******************************************************************************/
521
522 /**
523 * Takes a player and object count (tag) and returns the actual object
524 * pointer, or null if it can't be found.
525 */
526 object *
527 esrv_get_ob_from_count (object *pl, tag_t count)
528 {
529 if (pl->count == count)
530 return pl;
531
532 for (object *op = pl->inv; op; op = op->below)
533 if (op->count == count)
534 return op;
535 else if (op->type == CONTAINER && pl->container_ () == op)
536 for (object *tmp = op->inv; tmp; tmp = tmp->below)
537 if (tmp->count == count)
538 return tmp;
539
540 for (object *op = GET_MAP_OB (pl->map, pl->x, pl->y); op; op = op->above)
541 if (op->head && op->head->count == count)
542 return op;
543 else if (op->count == count)
544 return op;
545 else if (op->type == CONTAINER && pl->container_ () == op)
546 for (object *tmp = op->inv; tmp; tmp = tmp->below)
547 if (tmp->count == count)
548 return tmp;
549
550 #if 0
551 /* If the high bit is set, player examined a pseudo object. */
552 if (count & 0x80000000)
553 return 0;
554 #endif
555
556 return 0;
557 }
558
559 /** Client wants to examine some object. So lets do so. */
560 void
561 ExamineCmd (char *buf, int len, player *pl)
562 {
563 tag_t tag = atoi (buf);
564
565 /* If the high bit is set, player applied a pseudo object. */
566 if (tag & 0x80000000)
567 {
568 pl->ns->look_position = tag & 0x7fffffff;
569 pl->ns->floorbox_update ();
570 return;
571 }
572
573 object *op = esrv_get_ob_from_count (pl->ob, tag);
574
575 if (!op)
576 {
577 LOG (llevDebug, "Player '%s' tried to examine the unknown object (%ld)\n", &pl->ob->name, tag);
578 return;
579 }
580
581 examine (pl->ob, op);
582 }
583
584 /** Client wants to examine some object. So lets do so. */
585 void
586 ExCmd (char *buf, int len, player *pl)
587 {
588 tag_t tag = atoi (buf);
589
590 if (object *op = esrv_get_ob_from_count (pl->ob, tag))
591 {
592 std::string s = op->describe (pl->ob);
593
594 packet sl ("ex");
595 sl << ber32 (tag)
596 << data (*pl->expand_cfpod (s.c_str ()));
597
598 pl->ns->send_packet (sl);
599 }
600 }
601
602 /** Client wants to apply some object. Lets do so. */
603 void
604 ApplyCmd (char *buf, int len, player *pl)
605 {
606 tag_t tag = atoi (buf);
607
608 /* If the high bit is set, player applied a pseudo object. */
609 if (tag & 0x80000000)
610 {
611 pl->ns->look_position = tag & 0x7fffffff;
612 pl->ns->floorbox_update ();
613 return;
614 }
615
616 object *op = esrv_get_ob_from_count (pl->ob, tag);
617
618 if (!op)
619 {
620 LOG (llevDebug, "Player '%s' tried to apply the unknown object (%d)\n", &pl->ob->name, tag);
621 return;
622 }
623
624 player_apply (pl->ob, op, 0, 0);
625 }
626
627 /** Client wants to lock some object. Lets do so. */
628 void
629 LockItem (char *data, int len, player *pl)
630 {
631 int flag = data[0];
632 tag_t tag = net_uint32 ((uint8 *)data + 1);
633 object *op = esrv_get_ob_from_count (pl->ob, tag);
634
635 if (!op)
636 {
637 pl->failmsg ("Could not find object to lock/unlock");
638 return;
639 }
640
641 if (!flag)
642 CLEAR_FLAG (op, FLAG_INV_LOCKED);
643 else
644 SET_FLAG (op, FLAG_INV_LOCKED);
645
646 esrv_update_item (UPD_FLAGS, pl->ob, op);
647 }
648
649 /** Client wants to mark some object. Lets do so. */
650 void
651 MarkItem (char *data, int len, player *pl)
652 {
653 tag_t tag = net_uint32 ((uint8 *)data);
654 object *op = esrv_get_ob_from_count (pl->ob, tag);
655
656 if (!op)
657 {
658 pl->failmsg ("Could not find object to mark");
659 return;
660 }
661
662 pl->mark = op;
663 pl->ob->statusmsg (format ("Marked item %s", query_name (op)));
664 }
665
666 /**
667 * look_at prints items on the specified square.
668 *
669 * [ removed EARTHWALL check and added check for containers inventory.
670 * Tero.Haatanen@lut.fi ]
671 */
672 static void
673 look_at (player *pl, int dx, int dy)
674 {
675 dynbuf_text &buf = msg_dynbuf; buf.clear ();
676 object *ob = pl->ob;
677
678 if (!pl->observe->map)
679 return;
680
681 bool wiz = ob->flag [FLAG_WIZ] || ob->flag [FLAG_WIZLOOK];
682
683 mapxy pos (pl->observe);
684 pos.move (dx, dy);
685
686 if (wiz)
687 {
688 if (pos.normalise ())
689 {
690 mapspace &ms = *pos;
691 ms.update ();
692
693 buf.printf (" map: %s%+d%+d (%+d%+d)\n"
694 " smell %u/%u; flags %x; light %d; block %x; slow %x; on %x; off %x; volume %lld\n\n",
695 &pos.m->path, pos.x, pos.y, dx, dy,
696 (unsigned int)ms.smell, (unsigned int)mapspace::smellcount, ms.flags (), ms.light,
697 ms.move_block, ms.move_slow, ms.move_on, ms.move_off, (long long)ms.volume ()
698 );
699 }
700 else
701 buf << "off-map\n\n";
702 }
703
704 int darkness = pl->blocked_los (dx, dy);
705
706 if (darkness == LOS_BLOCKED)
707 buf << "You cannot see that place from your position. H<Something is in between or it is too far away.>";
708 else if (darkness == LOS_MAX)
709 buf << "That place is too dark to see anything. H<Move nearer or find better lighting.>";
710 else
711 {
712 if (pos.normalise ())
713 for (object *tmp = pos->top; tmp; tmp = tmp->below)
714 {
715 if (tmp->invisible && !QUERY_FLAG (ob, FLAG_WIZ))
716 continue;
717
718 if (wiz)
719 buf.printf (" - %s (%d).\n", query_name (tmp), tmp->count);
720 else
721 buf.printf (" - %s.\n", query_name (tmp));
722
723 object *head = tmp->head_ ();
724
725 if (head->inv)
726 if ((head->type != CONTAINER && head->type != FLESH)
727 || QUERY_FLAG (ob, FLAG_WIZ))
728 buf << head->query_inventory (ob, " ");
729
730 if (QUERY_FLAG (tmp, FLAG_IS_FLOOR) && !wiz) /* don't continue under the floor */
731 break;
732 }
733 }
734
735 if (buf.empty ())
736 buf << "You see nothing there.";
737
738 pl->infobox (MSG_CHANNEL ("lookat"), buf);
739 }
740
741 /** Client wants to look at some object. Lets do so. */
742 void
743 LookAt (char *buf, int len, player *pl)
744 {
745 char *cp;
746
747 int dx = atoi (buf);
748 if (!(cp = strchr (buf, ' ')))
749 return;
750
751 int dy = atoi (cp);
752
753 look_at (pl, dx, dy);
754 }
755
756 /** Move an object to a new location */
757 void
758 esrv_move_object (object *pl, tag_t to, tag_t tag, long nrof)
759 {
760 object *op = esrv_get_ob_from_count (pl, tag);
761 if (!op)
762 {
763 LOG (llevDebug, "Player '%s' tried to move an unknown object (%ld)\n", &pl->name, tag);
764 return;
765 }
766
767 if (!to)
768 { /* drop it to the ground */
769 if (op->map && !op->env)
770 return;
771
772 /* If it is an active container, then we should drop all objects
773 * in the container and not the container itself.
774 */
775 if (op->inv && QUERY_FLAG (op, FLAG_APPLIED))
776 {
777 int cnt = MAX_ITEM_PER_DROP;
778
779 for (object *current = op->inv; current && cnt--; )
780 {
781 object *next = current->below;
782 drop_object (pl, current, 0);
783 current = next;
784 }
785
786 if (cnt <= 0)
787 op->failmsg ("Only dropped some items, can't drop that many items at once.");
788 }
789 else
790 drop_object (pl, op, nrof);
791
792 return;
793 }
794 else if (to == pl->count)
795 { /* pick it up to the inventory */
796 /* return if player has already picked it up */
797 if (op->env == pl)
798 return;
799
800 pl->contr->count = nrof;
801 pick_up (pl, op);
802 return;
803 }
804
805 object *env = esrv_get_ob_from_count (pl, to);
806 if (!env)
807 {
808 LOG (llevDebug, "Player '%s' tried to move object to the unknown location (%d)\n", &pl->name, to);
809 return;
810 }
811
812 /* put_object_in_sack presumes that necessary sanity checking
813 * has already been done (eg, it can be picked up and fits in
814 * in a sack, so check for those things. We should also check
815 * an make sure env is in fact a container for that matter.
816 */
817 if (env->type == CONTAINER
818 && can_pick (pl, op)
819 && sack_can_hold (pl, env, op, nrof))
820 put_object_in_sack (pl, env, op, nrof);
821 }
822