ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/item.C
Revision: 1.67
Committed: Tue May 6 19:47:56 2008 UTC (16 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.66: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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